#include "stdafx.h"
#include "ObjectTracker.h"


//ObjectTracker::ObjectTracker(void)
//{

//           
//} 

bool sortfunction (distanceID i,distanceID j) { return (i.x<j.x); }  
struct ifoo_version_42 {
   long x, y, z;
   char *name;
   long a, b, c;
};      
void ObjectTracker::printMatchingFrames(IplImage* refImage)
{    
        DBConnect* dbConnect = new DBConnect("localhost","test","root","root");
   // cudaDeviceReset();
    //SURFeatures surf;  
    //DBConnect* dbConnect = new DBConnect("localhost","test","root","root");
    time_t timeTwoHoursAgo;

    timeTwoHoursAgo=time(0)-54*3600;
    
    vector<char*> ipAddressVector;
     /*ipAddressVector.push_back("192.248.10.0");
     ipAddressVector.push_back("192.248.11.0");
     ipAddressVector.push_back("192.248.12.0");   */
     
     ipAddressVector.push_back("192.248.10.0");
     ipAddressVector.push_back("192.248.11.0");
     ipAddressVector.push_back("192.248.12.0");
     
   // vector<IplImage*> imageVector= dbConnect->loadByCameraDate("192.248.1.0",&timeTwoHoursAgo);
    //vector<IplImage*> imageVector= dbConnect->loadByDate(&timeTwoHoursAgo);
 // vector<int> imageIDVector= dbConnect->loadImageIDByDate(&timeTwoHoursAgo);
 //ipAddressVector.pop_back();
 
 
 // vector<int> imageIDVector= dbConnect->loadImageIDByCameraDate(ipAddressVector[3],&timeTwoHoursAgo);
 
 /* Print the original 
 char currentCameraIp[11];
 vector<int> matchIdZeroImageIDVector= dbConnect->loadImageIDWithMatchIdZero();
 for(int i=0;i<matchIdZeroImageIDVector.size();i++)
 {
    int*  secFrom70int=new int();
     IplImage* imageNoMatchId = dbConnect->loadImageCameraIPById(currentCameraIp,secFrom70int,matchIdZeroImageIDVector[i]);
     
    char fileName[50];
    sprintf(fileName,"output\\%d_%s_%d.jpg",matchIdZeroImageIDVector[i],currentCameraIp,secFrom70int);
    cvSaveImage(fileName, imageNoMatchId);
    
    cvReleaseImage(&imageNoMatchId);
     delete secFrom70int; 
 } 
  */
 clock_t timeStart,timeEnd;
 
 vector<int> matchIdZeroImageIDVector= dbConnect->loadImageIDWithMatchIdZero();
 
 char currentCameraIp[11];
 char outputFileName[40];
 sprintf(outputFileName,"outputMatcher.txt");
 ofstream outPutFile;
 
  for(int i=0;i<matchIdZeroImageIDVector.size();i++)
  {
   /* if( matchIdZeroImageIDVector[i]>=28405&&matchIdZeroImageIDVector[i]<=29114)   //
    {
        continue;
    }*/
    
     /************************************ /
     if( matchIdZeroImageIDVector[i]>=28405&&matchIdZeroImageIDVector[i]<=29409)
    {
        continue;
    }
   
    if( matchIdZeroImageIDVector[i]>=29414&&matchIdZeroImageIDVector[i]<=29582)
    {
        continue;
    }
     if( matchIdZeroImageIDVector[i]>=29715&&matchIdZeroImageIDVector[i]<=29884)
    {
        continue;
    }
    if( matchIdZeroImageIDVector[i]>=30158&&matchIdZeroImageIDVector[i]<=30225)    //from 7_BC
    {
        continue;
    }
      /************************************/  
     timeStart = clock();
     cout<<matchIdZeroImageIDVector[i]<<endl;
     
      int*  secFrom70int=new int();
      IplImage* imageNoMatchId = dbConnect->loadImageCameraIPById(currentCameraIp,secFrom70int,matchIdZeroImageIDVector[i]);
     // time_t durationFrom(*secFrom70int-2*3600),durationTo(*secFrom70int);
      surf.setObject(imageNoMatchId);
      surf.extractSURFGPU(0);
     
      char* perviousCameraIp =0 ; 
      perviousCameraIp=getPerviousCameraIp(currentCameraIp);
      
      if((strcmp(perviousCameraIp, "0") != 0))
      {
          vector<int> imageIDVector= dbConnect->loadImageIDByCameraDuration(perviousCameraIp,(*secFrom70int-3600),*secFrom70int);   //was 3600//0.75
          vector<distanceID> distanceIDVector; 
          for(int j=0;j<imageIDVector.size();j++)
          {
           // distanceIDVector.push_back

               IplImage* imageTwo = dbConnect->loadImageById(imageIDVector[j]);
               float matchError = surf.matchSURFGPU(imageTwo);
               distanceID distanceIDVectorEliment;
               distanceIDVectorEliment.x = matchError;
               distanceIDVectorEliment.y = imageIDVector[j];
               distanceIDVector.push_back( distanceIDVectorEliment);
               // cvReleaseImage(&imageTwo);
             
          
          }
          std::sort (distanceIDVector.begin(), distanceIDVector.end(), sortfunction);
          if(distanceIDVector.size()>0)
          {
              char fileName[50];
              int  distanceIDVectorY =distanceIDVector[0].y; 
              sprintf(fileName,"output\\%d_%d.jpg",matchIdZeroImageIDVector[i],distanceIDVectorY);
              cvSaveImage(fileName, imageNoMatchId);
              
              IplImage* imageTwo = dbConnect->loadImageById(distanceIDVectorY);
              sprintf(fileName,"output\\%d_%f.jpg",matchIdZeroImageIDVector[i],distanceIDVector[0].x);
              cvSaveImage(fileName, imageTwo);
              //cvReleaseImage(&imageTwo);
          }
          distanceIDVector.clear();
          
      }
      
      delete secFrom70int; 
      
      timeEnd = clock();
      
     
      outPutFile.open (outputFileName, ios::app); 
      outPutFile<<"Time Taken:,"<<((timeEnd - timeStart)/(double)CLOCKS_PER_SEC)<<endl;
      outPutFile.close();
  }
     
 /*  vector<int> imageIDVector;
   imageIDVector.push_back(2070);
   imageIDVector.push_back(5042);
   imageIDVector.push_back(5040);
    imageIDVector.push_back(7043);*/
   
    
        //delete dbConnect; 
       /* char* currentCameraIp[11]=0;
        
        for(int i=0;i<matchIdZeroImageIDVector.size();i++)
        {
             IplImage* imageNoMatchId = dbConnect->loadImageCameraIPById(currentCameraIp,matchIdZeroImageIDVector[3]);
             getNextCameraIp( currentCameraIp);
            /* cvShowImage("NoMatchID",imageNoMatchId);
             cvWaitKey(0);* /
             
             
        } */
   
   /* if( imageIDVector.size()>0)
    {
       // DBConnect* dbConnect = new DBConnect("localhost","test","root","root");
        IplImage* imageOne = dbConnect->loadImageCameraIPById(currentCameraIp,imageIDVector[3]);
        surf.setObject(imageOne);
        cvShowImage("ref Image",imageOne);
        cvWaitKey(0);                                                        
        surf.extractSURFGPU(0);
          
    }
     delete dbConnect;*/

           /*if( currentCameraIp!=0)
           {
                DBConnect* dbConnect = new DBConnect("localhost","test","root","root");
               vector<int> tempImageIDVector= loadImageIDByCameraDate(ipAddressVector.,time_t* dateTime)
           }*/
    
   
                  

       //float* matchErrors = surf.matchSURFGPU(imageVector);
       
       //int* sortOrder = new int[imageVector.size()]; 
       
        /*for (int i=0; i<imageIDVector.size(); i++)
        {
            DBConnect* dbConnect = new DBConnect("localhost","test","root","root");
            IplImage* imageTwo = dbConnect->loadImageCameraIPById(currentCameraIp,imageIDVector[i]);
            float matchError = surf.matchSURFGPU(imageTwo);   
            
                  
                delete dbConnect;                 
                  
                   
          
        }
        
        surf.releaseSURFGPU(0);
        imageIDVector.clear();*/
        
     /*    std::sort(matchErrors,(matchErrors + imageVector.size()));
         
         for (int i=0; i<imageVector.size(); i++)
        {
            printf("%0.2f\n",matchErrors[i]);
        }*/
       
 //   for(std::vector<IplImage>::iterator it = imageVector.begin(); it != imageVector.end(); ++it)
   /* for (int i=0; i<imageVector.size(); i++)
    {
        IplImage* imageVectorItem= imageVector[i];
      //  IplImage imageVectorItem= (IplImage)*it;
         cvShowImage("From Vector",imageVectorItem);
         cvWaitKey(10);
        surf.setImage(imageVectorItem);  
        surf.extractSURFGPU(1);
        
        int noOfMatchers = surf.matchSURFGPU();
        cout<<"No Of Matchers :"<<noOfMatchers<<endl;
        
    }   */
    
  //  imageVector.clear();

    delete dbConnect;
}
char* ObjectTracker::getPerviousCameraIp(char* cameraIp)
{
    if (strcmp(cameraIp, "192.248.12.0") == 0)
    {
        return "192.248.11.0";
    }
    else if(strcmp(cameraIp, "192.248.11.0") == 0)
    {
       return "192.248.10.0";
    }
    else if(strcmp(cameraIp, "192.248.10.0") == 0)
    {
       return "0";
   }
    
    //printf("%c\n", cameraIp[9]);
    
    return "0";
}
ObjectTracker::~ObjectTracker(void)
{
      surf.~SURFeatures();
}           